<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/18 14:55
 * @version
 */
namespace Modules\Service\Logic;

use App\Models\MobileRecharge;
use App\Models\MobileRechargeOrder;
use App\Models\Pay;
use App\Models\User;
use App\Models\UserCoupon;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Modules\Token\Facades\Token;

class MobileFareOrderLogic extends BaseLogic{
    //充值支付
    public static function mobileOrderAndPay($mobile,$openid,$rechargeId,$payType,$rechargeAmount,$couponId,$userData){
        try {
            DB::beginTransaction();
            //如果openid存在,则修改用户openid
            if ($openid) {
                User::where("id", $userData->id)->update(["open_id" => $openid]);
            }
            //格式化钱符号
            $rechargeAmount = formatPrice($rechargeAmount);
            //定义参数
            $discount = '';
            $months = '';
            $type = 2;
            //查询充值套餐
            if ($rechargeId) {
                //查询套餐
                $rechargeData = MobileRecharge::find($rechargeId);
                if (!$rechargeData) return fail(1013, "充值套餐不存在");
                //赋值
                $discount = $rechargeData->discount;
                $months = $rechargeData->months;
                $type = 1;
                //计算套餐价格
                $rechargeAmount = formatPrice(($rechargeAmount * $rechargeData->months) * $rechargeData->discount);
            }
            //查询优惠券信息
            if ($couponId) {
                $couponData = UserCoupon::find($couponId);
                if (!$couponData) return fail(1016, "无效的优惠券,请重新选择");
                //查询用户充值记录
                $rechargeRecords = MobileRechargeOrder::where("user_id", $userData->id)->first();
                if ($rechargeRecords) {
                    //判断是否为新人专享优惠券
                    if ($couponData->new_exclusive == 1) {
                        return fail(1017, "优惠券为新人专享,请重新选择");
                    }
                }
                //计算价格
                $rechargeAmount = $rechargeAmount - $couponData->amount;
                $rechargeAmount = $rechargeAmount < 0 ? 0 : $rechargeAmount;
            }

            //实例化对象
            $order = new MobileRechargeOrder();
            $order->order_sn = orderSn();
            $order->user_id = $userData->id;
            $order->recharge_id = $rechargeId;
            $order->coupon_id = $couponId;
            $order->type = $type;
            $order->recharge_amount = $rechargeAmount;
            $order->mobile = $mobile;
            $order->discount = $discount;
            $order->months = $months;
            $order->user_mobile = $userData->mobile;
            $order->status = 0;
            $order->created_at = Carbon::now();

            //创建支付
            $pay = new Pay();
            $pay->payment_method = $payType;
            $pay->user_id = $userData->id;
            $pay->pay_sn = Pay::makeYouKaPaySN();
            $pay->amount = $rechargeAmount;
            $pay->save();

            $order->pay_id = $pay->id;//支付流水id
            $order->save();

            $response = [
                'order_id' => $order->id,
                'payment_method' => $payType,
                'pay_info' => null,
            ];

            $response['pay_info'] = '';
            $response['weixin_app'] = null;
            if ($payType == 'alipay') {
                $response['pay_info'] = Pay::alipay($pay);
            }
            if ($payType == 'weixin_app') {
                $response['weixin_app'] = Pay::weixinApp($pay);
            }
            if ($payType == 'weixin') {
                $response['weixin'] = Pay::weixin($pay, $openid);
            }

            DB::commit();
            return success($response);
        }catch (\Exception $e){
            Log::error($e);
            DB::rollBack();
            return fail(500,"系统错误");
        }
    }

    //订单再次支付
    public static function mobileOrderAgainPay($id,$payType){
        try {
            //查询订单
            $order = MobileRechargeOrder::find($id);
            //查询用户信息
            $user = User::find(Token::data('id'));
            //更改支付方式
            $pay = $order->pay;
            $pay->payment_method = $payType;
            $pay->save();

            $response = [
                'order_id' => $order->id,
                'payment_method' => $payType,
                'pay_info' => null,
            ];
            //支付序号
            if ($pay->pay_sn) {
                //$pay->pay_sn = Pay::makeYouKaPaySN();
            }
            $response['pay_info'] = '';
            $response['weixin_app'] = null;
            if ($payType == 'alipay') {
                $response['pay_info'] = Pay::alipay_num($pay);
            }
            if ($payType == 'weixin_app') {
                $response['weixin_app'] = Pay::weixinApp_num($pay);
            }
            if ($payType == 'weixin') {
                $response['weixin'] = Pay::weixin_num($pay, $user['open_id']);
            }
            return success($response);
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

}